/**
 * 
 */
package com.manage.service;

import java.util.List;

import org.apache.log4j.Logger;

import com.auth.model.Account;
import com.manage.config.Config;
import com.manage.model.CpAppChannel;
import com.service.AbstractService;
import com.util.ConfigUtil;
import com.utils.dbcp.ConnectionManager;
import com.utils.dbcp.ModelGenerator;


public class CpAppChannelServiceImpl extends AbstractService implements CpAppChannelService
{
	private Logger logger;
	
	public CpAppChannelServiceImpl()
	{
		logger = Logger.getLogger(this.getClass());
		this.dbcp = ConfigUtil.get(Config.SECTION, Config.KEY_DBCP_NAME);
	}

	@Override
	public List<CpAppChannel> getCpAppChannels(String appnumber) {
		String sql = "select * from cpappchannel where appnumber='"+appnumber+"'";
		ModelGenerator<CpAppChannel> dbcpmg = new ModelGenerator<CpAppChannel>(CpAppChannel.class);
		return dbcpmg.generateList(dbcp, sql);
	}

	@Override
	public boolean saveCpAppChannel(Account operator, String type,
			CpAppChannel cpappchannel) {
		if (operator == null || operator.getId() == null || operator.getId().length() == 0)
			return false;
		
		String sql = "";
		if (type.equalsIgnoreCase("insert"))
		{
			cpappchannel.setId(getTableId("cpappchannel"));
			sql = 
				"insert into cpappchannel(id,channelid,projectid,appid,appnumber,createuser,createdate) " +
				"values(" + String.valueOf(cpappchannel.getId()) + ",'" + cpappchannel.getChannelid() +
				"','" + cpappchannel.getProjectid() + "','" +
				cpappchannel.getAppid()+ "','" + cpappchannel.getAppnumber() + "','" + 
				cpappchannel.getCreateuser() + "','"+cpappchannel.getCreatedate()+"')";
			
		}
		else if (type.equalsIgnoreCase("update"))
		{
			
		}
		else if (type.equalsIgnoreCase("delete"))
		{
			sql = "delete from cpappchannel where id=" + String.valueOf(cpappchannel.getId()) + "";
		}
		else
		{
			sql = "";
		}
		logger.debug("sql: " + sql);
		if (sql.length() > 0)
		{
			if (ConnectionManager.getInstance().execute(dbcp, sql))
			{
				CpAppChannel lo = null;
				if (!type.equals("delete"))
				{
					lo = getCpAppChannelById(cpappchannel.getId());
				}
				logTableOperate("channel", type, operator, lo);
				return true;
			}
		}		
		return false;
	}
	
	@Override
	public CpAppChannel getCpAppChannelById(int id) {
		String sql = "select * from cpappchannel where id="+id+"";
		ModelGenerator<CpAppChannel> dbcpmg = new ModelGenerator<CpAppChannel>(CpAppChannel.class);
		return dbcpmg.generate(dbcp, sql);
	}

	@Override
	public List<CpAppChannel> getCpAppChannels() {
		String sql = "select * from cpappchannel";
		ModelGenerator<CpAppChannel> dbcpmg = new ModelGenerator<CpAppChannel>(CpAppChannel.class);
		return dbcpmg.generateList(dbcp, sql);
	}
	
	

}
